﻿// <copyright file="PaletteDefinition.cs" company="ThorIndustry">Copyright (c) ThorIndustry. All rights reserved.</copyright>

public class PaletteDefinition : IDatatableElement
{
    public ColorDefinition BackgroundColor
    {
        get;
        set;
    }

    public ColorDefinition HighlightColor
    {
        get;
        set;
    }

    public ColorDefinition HintColor
    {
        get;
        set;
    }

    [System.Xml.Serialization.XmlAttribute]
    public string Name
    {
        get;
        set;
    }

    public ColorDefinition SelectionColor
    {
        get;
        set;
    }

    [System.Xml.Serialization.XmlElement(ElementName = "ShapeColor", Type = typeof(ColorDefinition))]
    public ColorDefinition[] ShapeColors
    {
        get;
        set;
    }

    public class ColorDefinition
    {
        [System.Xml.Serialization.XmlIgnore]
        public UnityEngine.Color Color
        {
            get;
            set;
        }

        [System.Xml.Serialization.XmlAttribute("RGB")]
        public string XmlSerializableColor
        {
            get
            {
                return string.Format("{0},{1},{2},{3}", this.Color.r, this.Color.g, this.Color.b, this.Color.a);
            }

            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    return;
                }

                string[] tokens = value.Split(',');
                if (tokens.Length < 3)
                {
                    return;
                }

                float r = float.Parse(tokens[0]);
                float g = float.Parse(tokens[1]);
                float b = float.Parse(tokens[2]);
                float a = 1;
                if (tokens.Length == 4)
                {
                    a = float.Parse(tokens[3]);
                }

                if (r > 1 || g > 1 || b > 1)
                {
                    // We have to normalize the color...
                    r /= 255;
                    g /= 255;
                    b /= 255;
                }
                
                if (a > 1)
                {
                    a /= 255;
                }

                this.Color = new UnityEngine.Color(r, g, b, a);
            }
        }
    }
}
